package com.michaelbulava.TFS.ui.dialogs.connect;

import com.intellij.openapi.progress.ProgressManager;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.connect.ConnectToConfigurationServerTask;
import com.michaelbulava.TFS.ui.controls.connect.AddServerControl;
import com.michaelbulava.TFS.ui.dialogs.BaseDialog;
import com.microsoft.tfs.core.TFSConfigurationServer;
import com.microsoft.tfs.core.TFSConnection;
import com.microsoft.tfs.core.util.serverlist.ServerList;
import com.microsoft.tfs.core.util.serverlist.ServerListConfigurationEntry;
import com.microsoft.tfs.core.util.serverlist.ServerListEntryType;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.net.URI;
import java.text.MessageFormat;

/**
 * Created by mbulava on 6/15/2015.
 */
public class AddServerDialog extends BaseDialog {
    private AddServerControl addServerControl;
    private ServerList serverList;
    private TFSConnection connection;
    private URI serverURI;

    public AddServerDialog(@NotNull Component parent) {
        super(parent);
    }

    @Override
    protected Action[] createActions(){
        return new Action[]{getOKAction(), getCancelAction()};
    }

    @Override
    protected JComponent createControl() {
        if (addServerControl == null)
            addServerControl = new AddServerControl();
        return addServerControl;
    }

    @Override
    protected String dialogTitle() {
        return Messages.getString("AddServerDialog.AddServerDialogTitle");
    }

    @Override
    protected void doOKAction(){
        getOKAction().setEnabled(false);
        try{
            ConnectToConfigurationServerTask connectToProjectCollectionTask =
                    new ConnectToConfigurationServerTask(null, addServerControl.getServerURI(), null);

            ProgressManager.getInstance().run(connectToProjectCollectionTask);

            this.connection = connectToProjectCollectionTask.getConnection();
            this.serverURI = this.connection.getBaseURI();
            if ((this.serverList != null) && (this.serverList.contains(this.serverURI))){
                Messages.showErrorDialog(this.getRootComponent(), MessageFormat.format(Messages.getString(
                                "AddServerDialog.ServerExistsErrorFormat"), new Object[]{this.serverURI.getHost()}),
                        Messages.getString("AddServerDialog.ServerExistsErrorTitle"));
            }
        } finally {
            getOKAction().setEnabled(true);
        }
        super.doOKAction();
    }

    public TFSConnection getConnection(){
        return this.connection;
    }

    public URI getServerURI(){
        return this.serverURI;
    }

    public ServerListConfigurationEntry getServerListEntry(){
        ServerListEntryType type = (this.connection instanceof TFSConfigurationServer) ?
                ServerListEntryType.CONFIGURATION_SERVER : ServerListEntryType.TEAM_PROJECT_COLLECTION;
        return new ServerListConfigurationEntry(this.serverURI.getHost(), type, this.serverURI);
    }
}
